﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

public partial class masterdata_Form_List : PageBase
{
    ObjectTable ot;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitForm();
            btnSrh_Click(sender, e);
        }
    }

    private void InitForm()
    {
        GetManuType();
        GetCategoryType();
        GetBrandType();
    }

    protected void btnSrh_Click(object sender, EventArgs e)
    {
        GetFormBind();
    }

    #region 绑定品牌
    public void GetBrandType()
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ot = BrandManager.GetBrands(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlBrand.DataSource = ot.BillDataTable.DefaultView;
            ddlBrand.DataBind();
        }
        ddlBrand.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    #region 绑定制造商
    public void GetManuType()
    {
        ddlManu.DataTextField = "ManuName";
        ddlManu.DataValueField = "ManuCode";
        ot = ManufacturersManager.GetManus(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlManu.DataSource = ot.BillDataTable.DefaultView;
            ddlManu.DataBind();
        }
        ddlManu.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    #region 绑定品类
    public void GetCategoryType()
    {
        ddlCategroy.DataTextField = "CategoryName";
        ddlCategroy.DataValueField = "CategoryCode";
        ot = CategoryManager.GetCategories(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlCategroy.DataSource = ot.BillDataTable.DefaultView;
            ddlCategroy.DataBind();
        }
        ddlCategroy.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    private void GetFormBind()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(string.Format(" WHERE FM.DOMAIN_ID = {0} ", MyStateManager.DomainID));
        if (ddlBrand.SelectedValue.Length != 0)
        {
            sb.Append(string.Format(" AND BD.CODE='{0}' ", ddlBrand.SelectedValue));
        }

        if (ddlCategroy.SelectedValue.Length != 0)
        {
            sb.Append(string.Format(" AND CG.CODE='{0}' ", ddlCategroy.SelectedValue));
        }

        if (ddlManu.SelectedValue.Length != 0)
        {
            sb.Append(string.Format(" AND MA.CODE='{0}' ", ddlManu.SelectedValue));
        }
        if (txtFormName.Text.Trim() != "")
        {
            sb.Append(string.Format(" AND FM.NAME like '%{0}%' ", txtFormName.Text));
        }

        sb.Append(string.Format(" AND FM.ACTIVE={0}", chkActive.Checked?1:0));
        GridPager1.DataSource = new FormManager().GetFormDynamic(sb.ToString());
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
        GridPager1.Visible = GridView1.Rows.Count > 0;

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GetFormBind();
    }
}
